% ************************************************************************
% Simulation for the paper "The Perverse Consequences of Policy Restrictions
% in the Presence of Asymmetric Information"
% ************************************************************************
% PROOF OF PROPOSITION 2
% ************************************************************************

clear 
clc

% define parameters
lambda1=10;
lambda2=25;
p=0.5;
alpha=0.5;
w(1,1)=1;
w(2,1)=3.5;
w(3,1)=10;
step = 0.001;

for lambda = [lambda1 lambda2]
    W=0.45*w(1,1)+lambda/100*w(2,1)+(1-.45-(lambda/100))*w(3,1);
    t = (0:step:0.99);
    max_i=size(t,2);
    l = zeros(3,max_i);
    T = zeros(1,max_i);
    T_candidate = (0.01:0.001:W);
    max_k=size(T_candidate,2);
    outcome = zeros (1,max_k);

    % find equilibrium mapping between taxes and redistribution
    for i=1:max_i
    for k=1:max_k
    for j=1:3
        l(j,i)= (1-alpha)*(1+(T_candidate(1,k)/(w(j,1)*(1-t(1,i)))));
        if l(j,i)>1
            l(j,i)=1;
        end
    end
    revenue = t(1,i)*(0.45*w(1,1)*(1-l(1,i))+lambda/100*w(2,1)*(1-l(2,i))+(1-.45-(lambda/100))*w(3,1)*(1-l(3,i)));
    outcome(1,k)=abs(T_candidate(1,k)- revenue);
    end
    [aux,best_T]=min(outcome);
    T(1,i)=T_candidate(1,best_T);
    end

    % compute indirect utility functions and optimal leisure decisions 
    for i=1:max_i
    for j=1:3
        l(j,i)= (1-alpha)*(1+(T(1,i)/(w(j,1)*(1-t(1,i)))));
        if l(j,i)>1
            l(j,i)=1;
        end
        U(j,i) = alpha * log((1-t(1,i))*w(j,1)*(1-l(j,i))+T(1,i)) + (1-alpha)*log(l(j,i));
    end
    end

    % display preferred tax rates in the absence of restriction
    for j=1:3
        [aux,best(1,j)]=max(U(j,:));
        X = ['Optimal t for w=',num2str(w(j,1)),' when lambda=',num2str(lambda),': ',num2str(t(1,best(1,j)))];
        disp(X)
    end
    for j=1:3  % best(1,2) is the index of the equilibrium tax rate
        Y = ['Optimal l for w=',num2str(w(j,1)),' when median is mid-productivity and lambda=',num2str(lambda),': ',num2str(l(j,best(1,2)))];
        disp(Y)
    end
    
    % record optimal tax rates and leisure decisions in both states of the world
    if lambda==lambda1
        t1 = t(1,best(1,2));
        T1 = T(1,best(1,2));
        for j=1:3
            l1(j,1)=l(j,best(1,2));
        end
    elseif lambda==lambda2
        t2 = t(1,best(1,2));
        T2 = T(1,best(1,2));
        for j=1:3
            l2(j,1)=l(j,best(1,2));
        end
    end
    
    % plot figures
    figure
    subplot(2,1,1)
    plot(t,U(1,:),t,U(2,:),t,U(3,:));
    set(gca, 'XTick', [0 0.25 0.50 0.75 1]);
    set(gca, 'YTick', []);
    ylabel('indirect utility')
    subplot(2,1,2)
    plot(t,l(1,:),t,l(2,:),t,l(3,:));
    set(gca, 'XTick', [0 0.25 0.50 0.75 1]);
    set(gca, 'YTick', []);
    ylabel('leisure decision')
    xlabel('tax rate')
    L_L = ['low productivity (t*=',num2str(t(1,best(1,1))),')'];
    L_M = ['medium productivity (t*=',num2str(t(1,best(1,2))),')'];
    L_H = ['high productivity (t*=',num2str(t(1,best(1,3))),')'];
    legend(L_L,L_M,L_H)
end

% compute optimal l when uninformed and value of information
% first, order the optimal leisure decisions so that l1<l2
for j=1:3
    if (l1(j,1)==l2(j,1))
        lu(j,1)=l1(j,1);
    else
        if (l1(j,1)<l2(j,1))
            aux3=l1(j,1);
            aux4=l2(j,1);
        else
            aux3=l2(j,1);
            aux4=l1(j,1);
        end
        lu_aux = (aux3:step:aux4);
        max_k = size(lu_aux,2);
        objective = zeros (1,max_k);
        for k=1:max_k
            aux1= p    * (lu_aux(1,k)-l1(j,1))*(l2(j,1)-(1-alpha)*lu_aux(1,k));
            aux2=(1-p) * (lu_aux(1,k)-l2(j,1))*(l1(j,1)-(1-alpha)*lu_aux(1,k));
            objective(1,k)= abs(aux1+aux2);
        end
        [aux,ind_u]=min(objective);
        lu(j,1)=lu_aux(1,ind_u);
    end
    U1_uninf(j,1)=  alpha * log((1-t1)*w(j,1)*(1-lu(j,1))+T1) + (1-alpha)*log(lu(j,1));
    U2_uninf(j,1)=  alpha * log((1-t2)*w(j,1)*(1-lu(j,1))+T2) + (1-alpha)*log(lu(j,1));
    U1_inf(j,1)=  alpha * log((1-t1)*w(j,1)*(1-l1(j,1))+T1) + (1-alpha)*log(l1(j,1));
    U2_inf(j,1)=  alpha * log((1-t2)*w(j,1)*(1-l2(j,1))+T2) + (1-alpha)*log(l2(j,1));
    Delta(j,1) = p*(U1_inf(j,1) - U1_uninf(j,1)) + (1-p)*(U2_inf(j,1) - U2_uninf(j,1));
    X = ['Optimal l_u for w=',num2str(w(j,1)),': ',num2str(lu(j,1))];
    disp(X)    
    Y = ['Value of information for w=',num2str(w(j,1)),': ',num2str(Delta(j,1))];
    disp(Y)  
end

poor_voter_utility_NR=p*U1_inf(1,1) + (1-p)*U2_inf(1,1)

        
% optimal leisure decisions when we introduce a restriction of tr=0.38
% NOTE: costs of information are st everyone votes when there is no restriction 
%       yet less productive voters abstain in the presence of restriction.
%       This implies that richer voters are the median voters and will set the tax
%       rate equal to its minimal possible value (tR)

tR=0.38;

% this restriction ensures that l1=l2=1 for low productivity citizens
% this simplifies our computations...
% below we repeat the algorithm above with the restriction that t>tR

for lambda = [lambda1 lambda2]
    W=0.45*w(1,1)+lambda/100*w(2,1)+(1-.45-(lambda/100))*w(3,1);
    t = (tR:step:0.99);
    max_i=size(t,2);
    l = zeros(3,max_i);
    T = zeros(1,max_i);
    T_candidate = (0.01:0.001:W);
    max_k=size(T_candidate,2);
    outcome = zeros (1,max_k);

    % find equilibrium mapping between taxes and redistribution
    for i=1:max_i
    for k=1:max_k
    for j=1:3
        l(j,i)= (1-alpha)*(1+(T_candidate(1,k)/(w(j,1)*(1-t(1,i)))));
        if l(j,i)>1
            l(j,i)=1;
        end
    end
    revenue = t(1,i)*(0.45*w(1,1)*(1-l(1,i))+lambda/100*w(2,1)*(1-l(2,i))+(1-.45-(lambda/100))*w(3,1)*(1-l(3,i)));
    outcome(1,k)=abs(T_candidate(1,k)- revenue);
    end
    [aux,best_T]=min(outcome);
    T(1,i)=T_candidate(1,best_T);
    end

    % compute indirect utility functions and optimal l 
    for i=1:max_i
    for j=1:3
        l(j,i)= (1-alpha)*(1+(T(1,i)/(w(j,1)*(1-t(1,i)))));
        if l(j,i)>1
            l(j,i)=1;
        end
        U(j,i) = alpha * log((1-t(1,i))*w(j,1)*(1-l(j,i))+T(1,i)) + (1-alpha)*log(l(j,i));
    end
    end

    
    % display preferred tax rates in the absence of restriction
    for j=1:3
        [aux,best(1,j)]=max(U(j,:));
        X = ['RESTRICTION: Optimal t for w=',num2str(w(j,1)),' when lambda=',num2str(lambda),': ',num2str(t(1,best(1,j)))];
        disp(X)
    end
    for j=1:3  % best(1,2) is the index of the equilibrium tax rate
        Y = ['RESTRICTION: Optimal l for w=',num2str(w(j,1)),' when median is high-productivity and lambda=',num2str(lambda),': ',num2str(l(j,best(1,3)))];
        disp(Y)
    end
    
    % record optimal tax rates and leisure decisions in both states of the world
    if lambda==lambda1
        t1 = t(1,best(1,3));
        T1 = T(1,best(1,3));
        for j=1:3
            l1(j,1)=l(j,best(1,3));
        end
    elseif lambda==lambda2
        t2 = t(1,best(1,3));
        T2 = T(1,best(1,3));
        for j=1:3
            l2(j,1)=l(j,best(1,3));
        end
    end
end


% compute optimal l when uninformed and value of information when t>tR
% first order the optimal leisure decisions so that l1<l2

for j=1:3
    if (l1(j,1)==l2(j,1))
        lu(j,1)=l1(j,1);
    else
        if (l1(j,1)<l2(j,1))
            aux3=l1(j,1);
            aux4=l2(j,1);
        else
            aux3=l2(j,1);
            aux4=l1(j,1);
        end
        lu_aux = (aux3:step:aux4);
        max_k = size(lu_aux,2);
        objective = zeros (1,max_k);
        for k=1:max_k
            aux1= p    * (lu_aux(1,k)-l1(j,1))*(l2(j,1)-(1-alpha)*lu_aux(1,k));
            aux2=(1-p) * (lu_aux(1,k)-l2(j,1))*(l1(j,1)-(1-alpha)*lu_aux(1,k));
            objective(1,k)= abs(aux1+aux2);
        end
        [aux,ind_u]=min(objective);
        lu(j,1)=lu_aux(1,ind_u);
    end
    U1_uninf(j,1)=  alpha * log((1-t1)*w(j,1)*(1-lu(j,1))+T1) + (1-alpha)*log(lu(j,1));
    U2_uninf(j,1)=  alpha * log((1-t2)*w(j,1)*(1-lu(j,1))+T2) + (1-alpha)*log(lu(j,1));
    U1_inf(j,1)=  alpha * log((1-t1)*w(j,1)*(1-l1(j,1))+T1) + (1-alpha)*log(l1(j,1));
    U2_inf(j,1)=  alpha * log((1-t2)*w(j,1)*(1-l2(j,1))+T2) + (1-alpha)*log(l2(j,1));
    Delta(j,1) = p*(U1_inf(j,1) - U1_uninf(j,1)) + (1-p)*(U2_inf(j,1) - U2_uninf(j,1));
    X = ['RESTRICTION: Optimal l_u for w=',num2str(w(j,1)),': ',num2str(lu(j,1))];
    disp(X)    
    Y = ['RESTRICTION: Value of information for w=',num2str(w(j,1)),': ',num2str(Delta(j,1))];
    disp(Y)  
end

poor_voter_utility_R=p*U1_uninf(1,1) + (1-p)*U2_uninf(1,1)
loss = poor_voter_utility_NR- poor_voter_utility_R

